package com.qingyouchuxing.mall.biz.mapper;

import com.mybatisflex.core.BaseMapper;

import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryMethods;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.update.UpdateWrapper;
import com.mybatisflex.core.util.UpdateEntity;
import com.qingyouchuxing.mall.biz.domain.dto.MallGoodsDTO;
import com.qingyouchuxing.mall.biz.domain.entity.MallGoods;
import com.qingyouchuxing.mall.biz.domain.entity.MallGoodsSku;
import com.qingyouchuxing.mall.biz.domain.vo.MallGoodsVO;
import com.ruoyi.common.orm.core.page.PageQuery;
import org.apache.ibatis.annotations.Mapper;

import java.util.List;

import static com.qingyouchuxing.mall.biz.domain.entity.table.MallGoodsSkuTableDef.MALL_GOODS_SKU;
import static com.qingyouchuxing.mall.biz.domain.entity.table.MallGoodsSpuTableDef.MALL_GOODS_SPU;
import static com.qingyouchuxing.mall.biz.domain.entity.table.MallGoodsTableDef.MALL_GOODS;

/**
 * 商品Mapper接口
 *
 * @author 数据小王子
 * 2024-09-06
 */
@Mapper
public interface MallGoodsMapper extends BaseMapper<MallGoods>
{
    default Integer updateStatus(Long id, Integer status) {
        MallGoods mallGoods = UpdateEntity.of(MallGoods.class, id);
        // 通过 UpdateWrapper 操作 account 数据
        UpdateWrapper wrapper = UpdateWrapper.of(mallGoods);
        wrapper.set(MALL_GOODS.STATUS,status);
        return update(mallGoods);
    }
    default Page<MallGoodsVO> selectPage(MallGoodsDTO dto){
        QueryWrapper queryWrapper = QueryWrapper.create().from(MALL_GOODS)
            .select(MALL_GOODS.ALL_COLUMNS, QueryMethods.sum(MALL_GOODS_SKU.COUNT).as(MallGoodsVO::getCount))
            .leftJoin(MALL_GOODS_SPU).on(MALL_GOODS.ID.eq(MALL_GOODS_SPU.FK_GOODS_ID))
            .leftJoin(MALL_GOODS_SKU).on(MALL_GOODS_SPU.ID.eq(MALL_GOODS_SKU.FK_SPU_ID))
            .where(MALL_GOODS.ID.eq(dto.getId()))
            .and(MALL_GOODS.GOODS_NAME.like(dto.getGoodsName()))
            .and(MALL_GOODS.PRICE.eq(dto.getPrice()))
            .and(MALL_GOODS_SKU.COUNT.le(dto.getCount()))
            .and(MALL_GOODS.STATUS.eq(dto.getStatus()))
            .orderBy(dto.getOrderBy())
            .groupBy(MALL_GOODS.ID)
            ;
        return paginateAs(PageQuery.build(), queryWrapper, MallGoodsVO.class);
    }

    /**
     * 查询商品详情包含spu sku信息
     * @param id 商品id
     */
    default MallGoodsVO info(Long id){
//        MALL_GOODS_VO
//        QueryWrapper queryWrapper = QueryWrapper.create().from(MALL_GOODS_VO)
//            .select(MALL_GOODS_VO.ALL_COLUMNS,
//                MALL_GOODS_SPU.ID.as(MallGoodsSpuBO::getSpuId),MALL_GOODS_SPU.FK_GOODS_ID,MALL_GOODS_SPU.SPU_NAME,
//                MALL_GOODS_SKU.ID.as(MallGoodsSkuBO::getSkuId),MALL_GOODS_SKU.FK_SPU_ID,MALL_GOODS_SKU.COUNT,MALL_GOODS_SKU.DESCRIBE,MALL_GOODS_SKU.PRICE_FACTOR,MALL_GOODS_SKU.SKU_NAME
//            )
//            .leftJoin(MALL_GOODS_SPU).on(MALL_GOODS_VO.ID.eq(MALL_GOODS_SPU.FK_GOODS_ID))
//            .leftJoin(MALL_GOODS_SKU).on(MALL_GOODS_SPU.ID.eq(MALL_GOODS_SKU.FK_SPU_ID))
//            .where(MALL_GOODS_VO.ID.eq(id))
//            ;
        QueryWrapper queryWrapper = QueryWrapper.create().from(MALL_GOODS).where(MALL_GOODS.ID.eq(id));
        return selectOneByQueryAs(queryWrapper, MallGoodsVO.class);
    }
}
